import router from '@system.router'

var picker1value = null;
var picker2value = null;
var interval = null; // 间隔：较慢-6s，舒缓-4s，较快-2s
var timer1 = null;
var timer2 = null;
var counter = 0; // 预计切换次数
var timer3 = null;

export default {
    data: {
        seconds: 0,
        isshow: true,
        huxi: "吸气",
        percent: "0",
        duration: "",
        count: ""
    },
    clearAllInterval() {
        clearInterval(timer1);
        timer1 = null;
        clearInterval(timer2);
        timer2 = null;
        clearInterval(timer3);
        timer3 = null;
        this.percent = "100"
    },
    clickAction() {
        this.clearAllInterval();
        router.replace({
            uri: 'pages/index/index'
        })
    },
    run1() {
        this.seconds--;
        if (this.seconds == 0) {
            this.clearAllInterval();
            this.huxi = "已完成";
        }
    },
    run2() {
        counter++;
        if (counter == this.seconds / interval) {
            this.isshow = false; // 隐藏坚持秒数
        } else {
            if (this.huxi == "吸气") {
                this.huxi = "呼气"
            } else if (this.huxi == "呼气") {
                this.huxi = "吸气"
            }
        }
    },
    run3() {
        this.percent = (parseInt(this.percent) + 1).toString(); // 计数+1
        if (parseInt(this.percent) < 10) {
            this.percent = "0" + this.percent; // 小于10前面补0
        }
        if (this.percent == "100") {
            this.percent = "0";
        }
    },
    onInit() {
        console.log("xunlian's onInit()");
        console.log(this.data1);
        console.log(this.data2);

        picker1value = this.data1;
        picker2value = this.data2;

        if (picker1value == "1") {
            this.seconds = 60;
        } else if (picker1value == "2") {
            this.seconds = 120;
        } else if (picker1value == "3") {
            this.seconds = 180;
        }

        if (picker2value == "较慢") {
            interval = 6;
        } else if (picker2value == "舒缓") {
            interval = 4;
        } else if (picker2value == "较快") {
            interval = 2;
        }

        this.duration = interval + "s"; // duration必须带单位：s
        this.count = (this.seconds / interval).toString(); // 动画播放次数
    },
    onReady() {
        console.log("xunlian's onReady()");
    },
    onShow() {
        console.log("xunlian's onShow()");

        timer1 = setInterval(this.run1, 1000);
        timer2 = setInterval(this.run2, 1000 * interval);
        timer3 = setInterval(this.run3, interval * 1000 / 100);
    },
    onDestroy() {
        console.log("xunlian's onDestroy()");
    }
}
